﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using DLV.Data.Entities;
using DLV.FrameWork;

namespace DLV.BackEnd.Pages
{
    public partial class EditRoomType : AdminPageBase
    {
        private RoomTypeInfo _roomTypeInfo = new RoomTypeInfo();
        private readonly RoomTypeController _roomTypeController = new RoomTypeController();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["RoomTypeId"]))
                _roomTypeInfo = _roomTypeController.GetRoomType(int.Parse(Request.QueryString["RoomTypeId"]));

            if (Page.IsPostBack) return;

            LoadStatus();
            LoadUser();

            if (_roomTypeInfo.RoomTypeId == -1) return;

            RenderForm();
        }

        protected void lbtSave_Click(object sender, EventArgs e)
        {
            SaveData();
        }
        protected void lbtClose_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Pages/ListRoomType.aspx?xml=RoomType");
        }

        private void SaveData()
        {
            _roomTypeInfo.RoomTypeName = txtRoomTypeName.Text;
            _roomTypeInfo.Status = Convert.ToInt32(ddlStatus.SelectedValue);

            if (_roomTypeInfo.RoomTypeId == -1)
            {
                _roomTypeInfo.CreatedBy = ddlCreatedBy.SelectedValue == "-1"
                    ? UserId
                    : int.Parse(ddlCreatedBy.SelectedValue);
                _roomTypeInfo.CreatedDate = DateTime.Now;
                _roomTypeController.InsertRoomType(_roomTypeInfo);
            }
            else
            {

                _roomTypeInfo.UpdatedBy = UserId;
                _roomTypeInfo.UpdatedDate = DateTime.Now;
                _roomTypeController.UpdateRoomType(_roomTypeInfo);
            }

            Response.Redirect("/Pages/ListRoomType.aspx?xml=RoomType");
        }

        private void RenderForm()
        {
            txtRoomTypeName.Text = _roomTypeInfo.RoomTypeName;
            ddlStatus.SelectedValue = _roomTypeInfo.Status.ToString();
            ddlCreatedBy.SelectedValue = _roomTypeInfo.CreatedBy.ToString();
        }
        private void LoadStatus()
        {
            var ds = new DataSet();
            ds.ReadXml(MapPath("~/xml/Status.xml"));
            var dv = ds.Tables["Status"].DefaultView;
            ddlStatus.DataTextField = "Name";
            ddlStatus.DataValueField = "ID";
            ddlStatus.DataSource = dv;
            ddlStatus.DataBind();
        }
        private void LoadUser()
        {
            ddlCreatedBy.DataSource = new AdminUserController().ListAdminUser();
            ddlCreatedBy.DataTextField = "UserName";
            ddlCreatedBy.DataValueField = "UserId";
            ddlCreatedBy.DataBind();

            ddlCreatedBy.Items.Insert(0, new ListItem("--- Chọn người tạo ---", "-1"));
        }
    }
}